package com.ischoolbar.programmer.servlet;

/**
 * @Author lin
 **/

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.FileUploadBase;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import com.ischoolbar.programmer.dao.StudentDao;
import com.ischoolbar.programmer.dao.TeacherDao;
import com.ischoolbar.programmer.model.Student;
import com.ischoolbar.programmer.model.Teacher;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ProtocolException;
import java.util.Iterator;
import java.util.List;

public class PhotoServlet extends HttpServlet {
    private static final long serialVersionUID = 3274927179113071465L;

    public PhotoServlet() {
    }

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
        this.doPost(request, response);
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String method = request.getParameter("method");
        if ("getPhoto".equals(method)) {
            this.getPhoto(request, response);
        } else if ("SetPhoto".equals(method)) {
            this.uploadPhoto(request, response);
        }

    }

    private void uploadPhoto(HttpServletRequest request, HttpServletResponse response) {
        int sid = request.getParameter("sid") == null ? 0 : Integer.parseInt(request.getParameter("sid"));
        int tid = request.getParameter("tid") == null ? 0 : Integer.parseInt(request.getParameter("tid"));
        ServletFileUpload fileUpload = new ServletFileUpload(new DiskFileItemFactory());
        fileUpload.setSizeMax(20971520L);
        FileItemFactory fileItemFactory = new DiskFileItemFactory();
        fileUpload.setFileItemFactory(fileItemFactory);
        FileItem fileItem = fileItemFactory.createItem("photo", "image/jpg", true, "myphoto.jpg");
        fileUpload.setFileSizeMax(20971520L);
        response.setCharacterEncoding("UTF-8");

        try {
            List<FileItem> items = fileUpload.parseRequest(request);
            Iterator var9 = items.iterator();

            while(true) {
                while(var9.hasNext()) {
                    FileItem item = (FileItem)var9.next();
                    if (!item.isFormField() && ("image/jpg".equals(item.getContentType()) || "image/png".equals(item.getContentType()))) {
                        InputStream uploadInputStream = item.getInputStream();
                        if (sid != 0) {
                            Student student = new Student();
                            student.setId(sid);
                            student.setPhoto(uploadInputStream);
                            StudentDao studentDao = new StudentDao();
                            if (studentDao.setStudentPhoto(student)) {
                                response.getWriter().write("<div id='message'>上传成功！</div>");
                            } else {
                                response.getWriter().write("<div id='message'>上传失败！</div>");
                            }
                        }

                        if (tid != 0) {
                            Teacher teacher = new Teacher();
                            teacher.setId(tid);
                            teacher.setPhoto(uploadInputStream);
                            TeacherDao teacherDao = new TeacherDao();
                            if (teacherDao.setTeacherPhoto(teacher)) {
                                response.getWriter().write("<div id='message'>上传成功！</div>");
                            } else {
                                response.getWriter().write("<div id='message'>上传失败！</div>");
                            }
                        }
                    } else {
                        response.getWriter().write("<div id='message'>仅支持上传jpg和png格式的图片！</div>");
                    }
                }

                return;
            }
        } catch (ProtocolException var19) {
            try {
                response.getWriter().write("<div id='message'>上传协议错误！</div>");
            } catch (IOException var18) {
                var18.printStackTrace();
            }

            var19.printStackTrace();
        } catch (NullPointerException var20) {
            try {
                response.getWriter().write("<div id='message'>上传的文件为空!</div>");
            } catch (IOException var17) {
                var17.printStackTrace();
            }

            var20.printStackTrace();
        } catch (FileUploadBase.FileSizeLimitExceededException var21) {
            try {
                response.getWriter().write("<div id='message'>上传文件大小不能超过" + fileUpload.getFileSizeMax() + "！</div>");
            } catch (IOException var16) {
                var16.printStackTrace();
            }

            var21.printStackTrace();
        } catch (IOException var22) {
            try {
                response.getWriter().write("<div id='message'>读取文件出错！</div>");
            } catch (IOException var15) {
                var15.printStackTrace();
            }

            var22.printStackTrace();
        } catch (FileUploadException var23) {
            try {
                response.getWriter().write("<div id='message'>上传文件失败！</div>");
            } catch (IOException var14) {
                var14.printStackTrace();
            }

            var23.printStackTrace();
        }

    }

    private void getPhoto(HttpServletRequest request, HttpServletResponse response) {
        int sid = request.getParameter("sid") == null ? 0 : Integer.parseInt(request.getParameter("sid"));
        int tid = request.getParameter("tid") == null ? 0 : Integer.parseInt(request.getParameter("tid"));
        InputStream photo;
        byte[] b;
        if (sid != 0) {
            StudentDao studentDao = new StudentDao();
            Student student = studentDao.getStudent(sid);
            studentDao.closeCon();
            if (student != null) {
                photo = student.getPhoto();
                if (photo != null) {
                    try {
                        b = new byte[photo.available()];
                        photo.read(b);
                        response.getOutputStream().write(b, 0, b.length);
                    } catch (Exception var9) {
                        var9.printStackTrace();
                    }

                    return;
                }
            }
        }

        if (tid != 0) {
            TeacherDao teacherDao = new TeacherDao();
            Teacher teacher = teacherDao.getTeacher(tid);
            teacherDao.closeCon();
            if (teacher != null) {
                photo = teacher.getPhoto();
                if (photo != null) {
                    try {
                        b = new byte[photo.available()];
                        photo.read(b);
                        response.getOutputStream().write(b, 0, b.length);
                    } catch (Exception var10) {
                        var10.printStackTrace();
                    }

                    return;
                }
            }
        }

        String path = request.getSession().getServletContext().getRealPath("");
        File file = new File(path + "\\file\\logo.jpg");

        try {
            FileInputStream fis = new FileInputStream(file);
            b = new byte[fis.available()];
            fis.read(b);
            response.getOutputStream().write(b, 0, b.length);
        } catch (Exception var11) {
            var11.printStackTrace();
        }

    }
}

